package pku.ss.movieSystem.struts.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import pku.ss.movieSystem.services.IOrderService;

public class OrderManagerAction extends DispatchAction{

	private IOrderService orderService;
	
	/** 
	 * get All orders that have not been handled
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward getAllEffectOrders(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		try{
			request.setCharacterEncoding("utf8");
			response.setCharacterEncoding("utf8");
			
			PrintWriter out = response.getWriter();
			JSONObject jsonObj = new JSONObject();
			
			String orderInfo = orderService.getAllEffectOrdersJSONInfo();
			jsonObj.put("orders", orderInfo);
			out.print(jsonObj.toString());
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/** 
	 * delete certain order
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward deleteOrder(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		int orderId = Integer.parseInt(request.getParameter("orderid"));
		orderService.deleteOrderById(orderId);
		return null;
	}

	public void setOrderService(IOrderService orderService) {
		this.orderService = orderService;
	}
}
